package com.qys.livesMall.finance.service.impl;

import com.qys.livesMall.common.exception.enums.ExceptionEnum;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.finance.enums.AccountTypeEnum;
import com.qys.livesMall.finance.model.dto.AccountHandle;
import com.qys.livesMall.finance.model.dto.AccountInfo;
import com.qys.livesMall.finance.model.dto.FinancePayDTO;
import com.qys.livesMall.finance.model.dto.TransferAccountHandle;
import com.qys.livesMall.finance.model.dto.user.UserRWHandle;
import com.qys.livesMall.finance.service.FinanceBusinessService;
import com.qys.livesMall.finance.service.FinanceUserBusinessService;
import com.qys.livesMall.finance.service.TransferAccountsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @menu FinanceBusinessServiceImpl
 * @date 2023/07/29 17:37
 */
@Service
public class FinanceBusinessServiceImpl implements FinanceBusinessService {

    @Autowired
    private TransferAccountsService transferAccountsService;

    @Override
    public void pay(FinancePayDTO payDto) {
        payDto.setSuccess(false);
        if (payDto.getBusinessType()==null){
            payDto.setMessage(ExceptionEnum.PARAM_IS_ERROR);
        }
        //构建转账对象
        AccountInfo out=AccountInfo.build(payDto.getBusinessType().getOut(), payDto.getUserId(), payDto.getShopId());
        AccountInfo in=AccountInfo.build(payDto.getBusinessType().getIn(),payDto.getUserId(),payDto.getShopId());
        TransferAccountHandle transfer=TransferAccountHandle.build(out,in,payDto);
        //转账
        transferAccountsService.transfer(transfer);

        payDto.setSerialNumber(transfer.getSerialNumber());
        payDto.setSuccess(transfer.isSuccess());
        payDto.setMessage(transfer.getMessage());
    }
}

